package com.eascs.crm.nsEditDetail.controller;

import static com.eascs.site.view.HtmlRestResult.popHtmlFragment;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.internal.dao.PubDditemDao;
import com.eascs.crm.nsEditDetail.entity.EadbEabaCustomerNew;
import com.eascs.crm.nsEditDetail.form.EadbEabaCustomerForm;
import com.eascs.crm.nsEditDetail.service.CustomerNewService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sequence.SequenceService;
import com.eascs.workflow.common.service.sys.SysOrgService;

@RestController
@RequestMapping("/customerNewRest")
public class CustomerNewRestController extends BaseRestController {

	@Reference
	SysOrgService sysOrgService;
	
	@Autowired
	CustomerNewService customerNewService;
	
	@Reference
	private SequenceService sequenceService;
	
	
	@RequestMapping("/toShopNameAdd")
	public HtmlRestResult shopNameAdd() {
		//TODO
		List<ActOrgEntity> orgProvince = sysOrgService.getOrgProvince();
		ViewContext.put("ActOrgEntitys", orgProvince);
		return popHtmlFragment("customer/shop_name_add");
	}
	
	// 新增门店名称
		@RequestMapping("/ShopNameAdd")
		public BaseRestResult<String> ShopNameAdd(EadbEabaCustomerForm entity) {
			// vo.setCNAME("测试需要结果5");
			BaseRestResult<String> results = null;
			User                   user    = UserHolder.getUser();
			if (entity.getcName() != null && entity.getProvince() != null) {
				
				//code 的生成规则是什么
				
				String code         = generationCode("KH", entity.getProvince(), 6);
				
				//String provinceCode = findProvinceCode(entity.getProvince());

				EadbEabaCustomerNew eadbEabaCustomerNew = new EadbEabaCustomerNew();
				eadbEabaCustomerNew.setcType("2");
				eadbEabaCustomerNew.setcName(entity.getcName());
				eadbEabaCustomerNew.setcNames(entity.getcName());
				eadbEabaCustomerNew.setcCode(code);
				eadbEabaCustomerNew.setSourceType(1);
				eadbEabaCustomerNew.setSts("PAS");
				eadbEabaCustomerNew.setCreateByName(user.getCname());
				eadbEabaCustomerNew.setCreateBy(user.getNo());
				eadbEabaCustomerNew.setCreateTime(new Date(System.currentTimeMillis()));
				eadbEabaCustomerNew.setRemark("Manually_add");// 手动新增
				customerNewService.addEabaCustomer(eadbEabaCustomerNew);

				results = BaseRestResult.info("新增门店成功").andCloseActionDialog();
			} else {
				results = BaseRestResult.warn("门店或省份信息不能为空");
			}

			ViewContext.put("vo", entity);
			return results;
		}
		
		
		public String generationCode(String type, String province, int length) {
			String intoCabinetCode = "";
			if(type == null || province == null){
			throw new RuntimeException();
			}
			Calendar cal = Calendar.getInstance();
			String today = new SimpleDateFormat("yyyyMM").format(cal.getTime()); 
			// 根据省份查询省份简称
			
			String provinceJC = sysOrgService.getProvinceLetterCode(province);
			// 获取编号
			String createCode = sequenceService.getSeq(type, length);
			String splitCode[] = createCode.split("-");
			intoCabinetCode =  type + "-" + provinceJC + "-" + today + "-" + splitCode[1];
			return intoCabinetCode;
		}
	
		//根据省份查询省份简称
		/*public String findJCByProvince(String province) {
			PubDditem p=pubDditemDao.queryPubDditemNameCRM("25",province);
			return p.getItemCode();
		}*/
	/*	
		public String findProvinceCode(String province) {
			//pubDditemDao。queryPubDditemCRM()
			//String sql = "SELECT * FROM pub_dditem p WHERE p.item_name =? AND p.dd_no = '9'";
			// List<PubDditem> code= baseDao.findEntitys(sql, PubDditem.class, province);
			 PubDditem p=pubDditemDao.queryPubDditemNameCRM("9",province);
			 String provinceCode = "";
			if(p==null){
				if(province.equals("粤东")){
					provinceCode = "44";
				}else{
					provinceCode = "46";
				}
			}else{
				provinceCode = p.getItemCode();
			}
			return provinceCode;
		}*/
		
}